from pydantic_settings import BaseSettings
from typing import Optional, List
import os


class Settings(BaseSettings):
    """Application settings configuration."""
    
    # Application
    app_name: str = "教室借用智能审核系统"
    app_version: str = "0.1.0"
    debug: bool = False
    
    # Server
    host: str = "0.0.0.0"
    port: int = 8000
    reload: bool = False
    
    # CORS
    cors_origins: List[str] = ["*"]
    cors_credentials: bool = True
    cors_methods: List[str] = ["*"]
    cors_headers: List[str] = ["*"]
    
    # API Keys
    openai_api_key: Optional[str] = None
    openai_base_url: Optional[str] = None  # Custom base URL for OpenAI compatible APIs
    anthropic_api_key: Optional[str] = None
    
    # Authentication & Security
    api_key: Optional[str] = None  # Optional API key for authentication
    api_key_header: str = "X-API-Key"  # Header name for API key
    enable_auth: bool = False  # Enable API key authentication
    
    # Rate limiting
    enable_rate_limiting: bool = False  # Enable rate limiting
    rate_limit_requests: int = 100  # Requests per minute
    rate_limit_window: int = 60  # Time window in seconds
    
    # IP Whitelist
    ip_whitelist: List[str] = []  # IPs that bypass rate limiting
    
    # File Processing
    max_file_size: int = 10 * 1024 * 1024  # 10MB
    allowed_file_types: List[str] = [
        "text/markdown",
        "application/pdf", 
        "application/msword",
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        "application/vnd.ms-excel",
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        "image/jpeg",
        "image/png",
        "image/gif",
        "text/plain"
    ]
    upload_path: str = "uploads"
    
    # Cache Configuration
    enable_cache: bool = True  # Enable Redis caching
    redis_url: str = "redis://localhost:6379"  # Redis connection URL
    cache_ttl: int = 3600  # Default cache TTL in seconds (1 hour)
    max_cache_size: int = 1000  # Maximum cached items
    
    # File Cleanup Configuration
    enable_file_cleanup: bool = True  # Enable automatic file cleanup
    max_storage_size: int = 1024 * 1024 * 1024  # Max storage size (1GB)
    max_file_age_hours: int = 24  # Maximum file age before cleanup
    cleanup_interval_minutes: int = 60  # Cleanup interval in minutes
    enable_quota_management: bool = True  # Enable storage quota management
    
    # Database (for future expansion)
    database_url: Optional[str] = None
    
    # Logging
    log_level: str = "INFO"
    log_format: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    
    # Default classroom approval rules
    default_approval_rules: str = """
    教室借用审核标准：
    1. 申请用途必须明确且合理
    2. 申请时间不能与现有预订冲突
    3. 申请人身份验证有效
    4. 使用时长合理（一般不超过4小时）
    5. 特殊设备需求需要提前申请
    6. 商业用途需要额外审批
    7. 需要提供联系方式和责任人信息
    """
    
    model_config = {
        "env_file": ".env",
        "case_sensitive": False,
        "extra": "ignore"  # 忽略额外字段
    }


# Global settings instance
settings = Settings()